﻿namespace USElections.Converters
{
    using System;
    using System.Windows;
    using System.Windows.Data;
    using USElections.Model;

    public class HasImageVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Candidate candidate = value as Candidate;
            bool reverse = false;

            try
            {
                if (parameter != null)
                {
                    bool.TryParse(parameter.ToString(), out reverse);
                }
            }
            catch
            {
            }

            if (value != null)
            {
                if (string.IsNullOrEmpty(candidate.ImageUri))
                {
                    if (reverse)
                    {
                        return Visibility.Visible;
                    }
                    else
                    {
                        return Visibility.Collapsed;
                    }
                }
                else
                {
                    if (reverse)
                    {
                        return Visibility.Collapsed;
                    }
                    else
                    {
                        return Visibility.Visible;
                    }
                }
            }

            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
